package com.mobilesales.android.infrastructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.OverlayItem;
import com.mobilesales.android.R;
import com.mobilesales.android.util.MessageBox;

public class MapOverlays extends ItemizedOverlay {

	private Context context;
	private MapController mapController;

	private IPushpinSelected pushpinSelectedListener = null;	
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Map<OverlayItem,Boolean> visibilityMap = new HashMap<OverlayItem,Boolean>();
	private OverlayItem currentItem = null;
	
	private OverlayItem currentLocationItem = null;
	
	public MapOverlays(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	public MapOverlays(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		this.context = context;
	}
	
	public MapController getMapController() {
		return mapController;
	}

	public void setMapController(MapController mapController) {
		this.mapController = mapController;
	}
	
	public Context getContext() {
		return context;
	}

	public void setContext(Context mContext) {
		this.context = mContext;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		visibilityMap.put(overlay, true);
		populate();
	}
	
	public OverlayItem getItemFromGeoPoint(GeoPoint gp) {
		for(OverlayItem item : mOverlays) {
			if (item.getPoint().equals(gp)) {
				return item;
			}
		}
		return null;
	}
	
	public void setPushpinSelectedListener(IPushpinSelected pushpinSelectedListener) {
		this.pushpinSelectedListener = pushpinSelectedListener;
	}		
	
	public void setVisibility(GeoPoint gp, boolean visible) {
		OverlayItem item = this.getItemFromGeoPoint(gp);
		if (item!=null) {
			visibilityMap.put(item, visible);
			if (visible) {
				Drawable marker = getContext().getResources().getDrawable(R.drawable.pin_blue);
				item.setMarker(boundCenterBottom(marker));
			}
			else {
				if (item==currentItem)
					currentItem = null;
				
				Drawable marker = getContext().getResources().getDrawable(R.drawable.pin_invisible);
				item.setMarker(boundCenterBottom(marker));
			}
		}
	}
	
	public boolean getVisibility(GeoPoint gp) {
		OverlayItem item = this.getItemFromGeoPoint(gp);
		if (item!=null && visibilityMap.containsKey(item)) {
			return visibilityMap.get(item);
		}
		return false;
	}
	
	public void setCurrentItem(GeoPoint gp) {
		OverlayItem item = this.getItemFromGeoPoint(gp);
		setCurrentItem(item);
	}
	
	public void setCurrentItem(OverlayItem item) {
		if (item!=null) {
			if (currentItem!=null) {
				if (this.visibilityMap.containsKey(currentItem)) {
					boolean isVisible = this.visibilityMap.get(currentItem);
					if (isVisible) {
						Drawable marker = getContext().getResources().getDrawable(R.drawable.pin_blue);
						currentItem.setMarker(boundCenterBottom(marker));
					}
				}
				
			}
			currentItem = item;
			Drawable marker = getContext().getResources().getDrawable(R.drawable.pin_yellow);
			currentItem.setMarker(boundCenterBottom(marker));		
		}		
	}

	public void setCurrentLocation(GeoPoint currentLocation) {
		if (currentLocationItem==null) {
			String str = context.getString(R.string.MyLocation);
			currentLocationItem = new OverlayItem(currentLocation, str, str);
			Drawable marker = getContext().getResources().getDrawable(R.drawable.pin_red);
			currentLocationItem.setMarker(boundCenterBottom(marker));
			addOverlay(currentLocationItem);
		}
	}	
	
	public OverlayItem getCurrentLocationItem() {
		return currentLocationItem;
	}

	public OverlayItem getCurrentItem() {
		return currentItem;
	}

	@Override
	protected boolean onTap(int index) {
		if (getContext()!=null && mapController!=null) {
			OverlayItem item = mOverlays.get(index);
			if (item!=null && visibilityMap.containsKey(item)) {
				boolean isVisible = visibilityMap.get(item);
				if (isVisible) {
					if (item!=currentLocationItem) {
						setCurrentItem(item);
						if (pushpinSelectedListener!=null)
							pushpinSelectedListener.OnPushpinSelected(item.getPoint());
					}
					mapController.animateTo(item.getPoint());
					MessageBox.show(getContext(), item.getTitle());
				}
			}
		}
		return true;
	}

}
